<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
import { DeletePatientAPI, GetPatientMyListAPI } from '@/apis/patient'
import { useConsultStore } from '@/stores/consult'
import type { PatientItem } from '@/types/patient'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])
const patientList = ref<PatientItem[]>([])
const getPatientList = async () => {
  const res = await GetPatientMyListAPI()
  patientList.value = res.data
}
//获取患者列表
onShow(() => {
  getPatientList()
})
const onSwipeActionClick = async (id: string, index: number) => {
  console.log(id, index)
  //前端删除
  patientList.value.splice(index, 1)
  //后端删除
  await DeletePatientAPI(id)
  //刷新列表
  getPatientList()
  if (props.type === 'consult') {
    store.patientId = ''
  }
  //提示
  uni.showToast({
    title: '删除成功',
    icon: 'success',
  })
}
const props = defineProps<{
  type: string
}>()
const setNavigationBarTitle = () => {
  if (props.type === 'consult') {
    uni.setNavigationBarTitle({ title: '选择患者' })
  } else {
    uni.setNavigationBarTitle({ title: '患者列表' })
  }
}
onLoad(() => {
  setNavigationBarTitle()
})
const store = useConsultStore()
const handleSelectPatient = (id: string) => {
  if (props.type !== 'consult') return
  store.patientId = id
}
const handleNextType = () => {
  uni.navigateTo({ url: '/subpkg_consult/pages/payment/payment' })
}
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          :right-options="swipeOptions"
          v-for="(item, index) in patientList"
          :key="item.id"
          @click="onSwipeActionClick(item.id, index)"
        >
          <view
            class="archive-card"
            @click="handleSelectPatient(item.id)"
            :class="{
              active:
                props.type === 'consult' ? store.patientId === item.id : item.defaultFlag === 1,
            }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">
                {{ item.idCard.slice(0, 3) }}***********{{ item.idCard.slice(-4) }}
              </text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/pages/form/form?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/pages/form/form">
          <uni-icons color="#16C2A3" size="24" type="compose" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
    <button
      class="uni-button"
      v-if="props.type === 'consult'"
      :disabled="store.patientId == ''"
      @click="handleNextType"
    >
      下一步
    </button>
  </scroll-page>
</template>

<style lang="scss">
@import './index.scss';
</style>
